require(["config"], function() {
	require(["jquery", "cookie","header"], function($) {
		function Confirm() {
			this.verify();
			this.loadAddress();
		}

		$.extend(Confirm.prototype, {
			verify: function() {
				$.cookie.json = true;
				var user = $.cookie("login-user");
				if (!user) // 用户未登录
					location = "/html/login.html";
			},
			// 加载地址数据
			loadAddress: function() {
				// 首先加载省份数据
				this.loadProvince();
				// 省份选择改变时，加载城市
				$(".province").on("change", this.loadCity);				
				// 城市选择改变时，加载区县
				$(".city").on("change", this.loadDistrict);
			},
			// 加载省份			

			loadProvince: function() {
				var url1 = "http://route.showapi.com/1149-1?showapi_appid=75639&showapi_sign=a8fbfd277d6549dba24aa45b4794cc47&level=1&page=1&maxSize=20",
					  url2 = "http://route.showapi.com/1149-1?showapi_appid=75639&showapi_sign=a8fbfd277d6549dba24aa45b4794cc47&level=1&page=1&maxSize=20";
				$.when($.ajax(url1), $.ajax(url2))
				  .done(function(data1, data2) {
				  	console.log(arguments)
				  	var html = '<option value="-1">请选择省份</option>';
				  	data1[0].showapi_res_body.data
				  		.concat(data2[0].showapi_res_body.data)
				  		.forEach(function(curr) {
				  			html += `<option value="${curr.id}">${curr.areaName}</option>`;
				  		});
				  	$(".province").html(html);
				  });
			},
			// 加载城市
			loadCity: function() {
				// 获取选择的省份 id
				var id = $(".province").val();
				// 根据 id 查询城市
				var url = `http://route.showapi.com/1149-2?showapi_appid=75639&showapi_sign=a8fbfd277d6549dba24aa45b4794cc47&parentId=${id}`;
				$.ajax(url).done(function(data) {
					var html = '<option value="-1">请选择城市</option>';
					data.showapi_res_body.data.forEach(function(curr) {
						html += `<option value="${curr.id}">${curr.areaName}</option>`;
					});
					$(".city").html(html);
				});
			},
			// 加载区县
			loadDistrict: function() {
				// 获取选择的城市 id
				var id = $(".city").val();
				// 根据 id 查询区县
				var url = `http://route.showapi.com/1149-2?showapi_appid=75639&showapi_sign=a8fbfd277d6549dba24aa45b4794cc47&parentId=${id}`;
				$.ajax(url).done(function(data) {
					var html = '<option value="-1">请选择区县</option>';
					data.showapi_res_body.data.forEach(function(curr) {
						html += `<option value="${curr.id}">${curr.areaName}</option>`;
					});
					$(".district").html(html);
				});
			}
		});

		new Confirm();
	});
});